let qaid;
onload = function (){

    liang();
    daohanglan();
    var sheng=document.querySelector("#sheng");

    let html=``
    for (var i = 0; i < citys.length; i++) {
        html+=`<div onclick="clicksheng(`+i+`)"  class="sheng" style="background-color: #dddcdc;border-radius: 5px;padding: 10px;">`+(citys[i])["n"]+`</div>`;
    }
    // citys.forEach(c=>{
    // 	html+=`<div  class="sheng" style="background-color: #0381fe;border-radius: 5px;padding: 10px;">`+c["n"]+`</div>`;
    // });
    sheng.innerHTML=html;
    var shengdiv=document.querySelectorAll(".sheng");


    document.querySelector("#search").addEventListener("keydown",keyboard)
    document.querySelector("#search").addEventListener("input",searchtips)
    document.querySelector(".searchtip").addEventListener("mouseleave",function (){
        document.querySelector(".searchtip").style.display="none"
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("mouseover",xllijdbc);
    })
    a =document.querySelector(".showNavtwoson").innerHTML;
    document.querySelector(".newtuijian").addEventListener("mouseover",function (){
        document.querySelector(".showNavtwoson").innerHTML=a;
    })
    document.querySelectorAll(".gnb_nav_center").forEach(x=>{
        x.addEventListener("mouseover",function (){
            document.querySelector(".showNavtwoson").innerHTML=a;
        })
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("click",function (){
            let xid = parseInt(event.target.getAttribute("id"));
            location.href="/goodsList?xid="+xid+"&show="+1;
        })
    })




    document.querySelector("#search").addEventListener("keydown",keyboard)
    document.querySelector("#search").addEventListener("input",searchtips)
    document.querySelector(".searchtip").addEventListener("mouseleave",function (){
        document.querySelector(".searchtip").style.display="none"
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("mouseover",xllijdbc);
    })
    a =document.querySelector(".showNavtwoson").innerHTML;
    document.querySelector(".newtuijian").addEventListener("mouseover",function (){
        document.querySelector(".showNavtwoson").innerHTML=a;
    })
    document.querySelectorAll(".gnb_nav_center").forEach(x=>{
        x.addEventListener("mouseover",function (){
            document.querySelector(".showNavtwoson").innerHTML=a;
        })
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("click",function (){
            let xid = parseInt(event.target.getAttribute("id"));
            location.href="/goodsList?xid="+xid+"&show="+1;
        })
    })
}



function keyboard(){
    let keycode = event.keyCode;
    if (keycode == 13){
        search();
    }
}

function search(){
    let searchvalue = document.querySelector("#search").value;
    if(searchvalue==""){
        event.preventDefault();
    }else {
        location.href="/index?v=search&searchvalue="+searchvalue;
    }

}

function keyboard(){
    let keycode = event.keyCode;
    if (keycode == 13){
        search();
    }
}

function search(){
    let searchvalue = document.querySelector("#search").value;
    if(searchvalue==""){
        event.preventDefault();
    }else {
        location.href="/index?v=search&searchvalue="+searchvalue;
    }

}

function searchtips(){
    if (document.querySelector("#search").value==""){
        document.querySelector(".searchtip").style.display="none"
    }else {
        document.querySelector(".searchtip").style.display="block"
    }
    let searchvalue = document.querySelector("#search").value;
    $.ajax({
        url:"/index?v=searchtip",
        type:"post",
        data:{
            searchvalue
        },
        dataType:"json",
        success:(resp=>{
            if (resp.length>4){
                document.querySelector(".searchtip").style.height="80px";
                document.querySelector(".searchtip").style.overflowY="scroll";
            }else if (resp.length==0){
                document.querySelector(".searchtip").style.height="20px";
                document.querySelector(".searchtip").style.overflowY="";
            }else if(resp.length<4){
                document.querySelector(".searchtip").style.height="auto";
                document.querySelector(".searchtip").style.overflowY="";
            }
            let tipsbox = document.querySelector(".searchtip");
            let html=``;
            resp.forEach(r=>{
                html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
            })
            tipsbox.innerHTML=html;
        })
    })
}

function bindfuture(){
    let divname = event.currentTarget.innerText;
    location.href="/index?v=search&searchvalue="+divname;
}


function xllijdbc(){
    let xid = parseInt(event.currentTarget.getAttribute("id"));
    $.ajax({
        url:"/index?v=searchli",
        type:"post",
        data:{
            xid
        },
        success:((resp)=>{
            let r = JSON.parse(resp);
            let showNavtwoson = document.querySelector(".showNavtwoson");
            let html = ``;
            r.forEach(x=>{
                x.commodities.forEach(y=>{
                    html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+x.lname+`</li>`
                })
            })
            showNavtwoson.innerHTML=html;
        })
    })
}

function chuandetail(params1,params2){
    location.href="/productDetails?sid="+params1+"&lid="+params2;
}

function searchtips(){
    if (document.querySelector("#search").value==""){
        document.querySelector(".searchtip").style.display="none"
    }else {
        document.querySelector(".searchtip").style.display="block"
    }
    let searchvalue = document.querySelector("#search").value;
    $.ajax({
        url:"/index?v=searchtip",
        type:"post",
        data:{
            searchvalue
        },
        dataType:"json",
        success:(resp=>{
            if (resp.length>4){
                document.querySelector(".searchtip").style.height="80px";
                document.querySelector(".searchtip").style.overflowY="scroll";
            }else if (resp.length==0){
                document.querySelector(".searchtip").style.height="20px";
                document.querySelector(".searchtip").style.overflowY="";
            }else if(resp.length<4){
                document.querySelector(".searchtip").style.height="auto";
                document.querySelector(".searchtip").style.overflowY="";
            }
            let tipsbox = document.querySelector(".searchtip");
            let html=``;
            resp.forEach(r=>{
                html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
            })
            tipsbox.innerHTML=html;
        })
    })
}

function bindfuture(){
    let divname = event.currentTarget.innerText;
    location.href="/index?v=search&searchvalue="+divname;
}
function bindfuturenew(){
    let divname = event.currentTarget.innerText;
    let divnames = divname.substr(0,divname.length-1);
    location.href="/index?v=search&searchvalue="+divnames;
}

function xllijdbc(){
    let xid = parseInt(event.currentTarget.getAttribute("id"));
    $.ajax({
        url:"/index?v=searchli",
        type:"post",
        data:{
            xid
        },
        success:((resp)=>{
            let r = JSON.parse(resp);
            let showNavtwoson = document.querySelector(".showNavtwoson");
            let html = ``;
            r.forEach(y=>{
                html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+y.sname+`</li>`
            })
            showNavtwoson.innerHTML=html;
        })
    })
}

function Login(){
    let login = document.querySelector("#Login");
    let levitation = document.querySelector("#levitation");

    login.onmouseenter = function (){
        levitation.style.display = "block";
    }
    levitation.onmouseleave = function (){
        levitation.style.display = "none";
    }
}
function isnull(){
    location.href="/login"
}
function iscart(){
    location.href="/goods"
}
function isdefault(aid){
    var rec1 = document.querySelectorAll(".receiving-isdefault");

    rec1.forEach(x=>{
        let m = event.target;
        console.log(m)
        if (m==x){
            x.style.background = "#deecfc";
            x.style.borderColor = "#deecfc";
            x.style.color = "#54a3fc"
        }else{
            x.style.background = "";
            x.style.borderColor = "#000000";
            x.style.color = "#000000"
        }
    })
    $.ajax({
        url:"/Shippingaddress?v=Upisdefault",
        type:"post",
        data:{
            aid
        },
        success:function (r){
            if (r>0){
                alert("设置默认地址成功")
            }
        }
    })
}

function deladdress(aid){
    if (confirm("是否确定删除当前地址？")){
        let a = event.target.parentNode.parentNode;
        a.parentNode.removeChild(a);
        $.ajax({
            url:"/Shippingaddress?v=delAddress",
            type:"post",
            data:{
                aid
            },
            success:function (r){
                if(r>0){
                    alert("删除成功！");
                }
            }
        })
    }
}

function clicksheng(index){
    // var c=event.currentTarget.innerHTML;
    console.log((citys[index])["n"]);
    var shi=document.querySelector("#shi");
    let html=``;

    for (var i = 0; i < ((citys[index])["c"]).length; i++) {
        console.log((((citys[index])["c"])[i])["a"])
        html+=`<div class="shi" onclick="clickshi(`+index+`,`+i+`)"  class="sheng" style="background-color: #dddcdc;border-radius: 5px;padding: 10px;">`+(((citys[index])["c"])[i])["n"]+`</div>`
    }
    shi.innerHTML=html;
    var shidiv=document.querySelectorAll(".shi");
    shidiv[0].onclick()
}

function clickshi(a,b){
    console.log((((citys[a])["c"])[b])["n"]);
    var qu=document.querySelector("#qu");
    let html=``;
    for (var i = 0; i < (((citys[a])["c"])[b])["a"].length; i++) {
        console.log()
        html+=`<div onclick="clickqu(`+a+`,`+b+`,`+i+`)"  class="sheng" style="background-color: #dddcdc;border-radius: 5px;padding: 10px;">`+((((citys[a])["c"])[b])["a"])[i]+`</div>`

    }
    qu.innerHTML=html;

}
function clickqu(a,b,c){

    console.log(((((citys[a])["c"])[b])["a"])[c]);
    var nbi = document.querySelector(".nad-body-input2");
    nbi.innerHTML = (citys[a])["n"]+"/"+(((citys[a])["c"])[b])["n"]+"/"+((((citys[a])["c"])[b])["a"])[c];
    var addressselect=document.querySelector("#addressselect");
    addressselect.style.display="none";
}
function selectaddress(){
    var addressselect=document.querySelector("#addressselect");
    addressselect.style.display="block";
}
function liang(){
    let div = document.querySelectorAll(".PaymentMethod");
    div.forEach(a=>{
        a.addEventListener("click",function(){
            div.forEach(x=>{
                x.style.borderColor = "#c5c5c5";
            })
            event.currentTarget.style.borderColor = "#43a1f6";
        })

    })
}

function daohanglan() {
    let allLi = document.querySelectorAll(".gnb_nav_center li");
    let show = document.querySelector("#show");
    let zzc = document.querySelector("#zzc");
    let img = document.querySelectorAll(".showNavone li span img");
    let showli = document.querySelectorAll(".showNavone li");
    let showNavtwoson = document.querySelector(".showNavtwoson");

    allLi.forEach(x => {
        x.addEventListener("mouseover", function() {
            document.querySelector("#show").style.height = 680 + "px";
            zzc.style.display = "block";
            console.log(document.querySelector("#show").style.height)
            showli.forEach(x => {
                x.style.fontWeight = "";
            })

            for (var i = 0; i < img.length; i++) {
                if (i == 0) {
                    img[i].style.display = "block";
                    document.querySelector(".showNavone li").style
                        .fontWeight = "bold";
                } else {
                    img[i].style.display = "none";
                }
            }
            allLi.forEach(y => {
                y.style.background = "#fff";
                y.style.color = "#000";
            })
            event.target.style.background = "#000";
            event.target.style.color = "#ffff";

            showNavtwoson.style.display = "block";
        })

    })
    document.querySelector(".gnb_nav").onmouseleave = function() {
        show.style.height = 0 + "px";
        zzc.style.display = "none";
        allLi.forEach(y => {
            y.style.background = "#fff";
            y.style.color = "#000";
        })
    }

    let cancel = document.querySelector("#cancelshow");
    cancel.addEventListener("click", function() {
        show.style.height = 0 + "px";
    })

    showli.forEach(x => {
        x.addEventListener("mouseover", function() {
            img.forEach(x => {
                x.style.display = "none";
            })
            let hhh = event.currentTarget.querySelector("span img");
            hhh.style.display = "block";
            showli.forEach(y => {
                y.style.fontWeight = "";
            })
            event.currentTarget.style.fontWeight = "bold";

            showNavtwoson.style.display = "none";
            setTimeout(() => {
                showNavtwoson.style.display = "block"
            }, 150);
        })
    })
}


function newaddress(){
    let e = document.querySelector(".nad-body-btn2");
    e.removeEventListener('click',queding2);
    e.onclick = queding;
    let a = document.querySelector(".newaddressbox");
    a.style.display = "block";
}


function imgdelbtn(){
    let e = document.querySelector(".nad-body-btn2");
    e.removeEventListener('click',queding2);
    e.onclick = queding;
    let f = document.querySelector(".nad-title-text");
    f.innerHTML = "新建收货地址";
    let a = document.querySelector(".newaddressbox");
    let b = document.querySelectorAll(".nad-body-input1");
    let c = document.querySelector(".nad-body-input2");
    let d = document.querySelector(".nad-body-input3")
    b[0].value = "";
    b[1].value = "";
    c.innerHTML = "选择省份/市区";
    d.value = "";
    a.style.display = "none";
}

function quxiao(){
    let e = document.querySelector(".nad-body-btn2");
    e.removeEventListener('click',queding2);
    e.onclick = queding;
    let f = document.querySelector(".nad-title-text");
    f.innerHTML = "新建收货地址";
    let a = document.querySelector(".newaddressbox");
    let b = document.querySelectorAll(".nad-body-input1");
    let c = document.querySelector(".nad-body-input2");
    let d = document.querySelector(".nad-body-input3")
    b[0].value = "";
    b[1].value = "";
    c.innerHTML = "选择省份/市区";
    d.value = "";

    a.style.display = "none";
}
function queding(){
    let addressname;
    let sheng;
    let shi;
    let qu;
    let aname;
    let aphone;
    let a = document.querySelectorAll(".nad-body-input1");
    let b = document.querySelector(".nad-body-input2");
    let c = document.querySelector(".nad-body-input3");
    console.log(a[0].value);
    if (a[0].value!=""){
        aname = a[0].value;
    }else{
        alert("请输入正确名字");
        return null;
    }

    if ((/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(a[1].value)){
        aphone = a[1].value;
    }else{
        alert("请输入正确的手机号");
        return null;
    }
    if (b.innerText!="选择省份/自治区"){
        var strings = b.innerHTML.split("/");
        sheng = strings[0];
        shi = strings[1];
        qu = strings[2];
    }else {
        alert("请选择省市区");
        return null;
    }

    if (c.value!=""){
        addressname= c.value;
    }else{
        alert("请输入详细地址");
        return null;
    }

    $.ajax({
        url: "/Shippingaddress?v=addnewAddress",
        type: "post",
        data: {
            addressname,
            sheng,
            shi,
            qu,
            aname,
            aphone
        },
        success:(r=>{
            let a = document.querySelector(".newaddressbox");
            if (r=="成功添加地址！"){
                alert(r);
                $("#adressd").load(location.href + " #adressd>*","");
                a.style.display = "none";
            }else{
                alert(r);
            }

        })
    })
}

function queding2(){
    console.log("xxxxxxxxxxxx");
    let addressname;
    let sheng;
    let shi;
    let qu;
    let aname;
    let aphone;
    let jaid = qaid;
    let a = document.querySelectorAll(".nad-body-input1");
    let b = document.querySelector(".nad-body-input2");
    let c = document.querySelector(".nad-body-input3");
    console.log(a[0].value);
    if (a[0].value!=""){
        aname = a[0].value;
    }else{
        alert("请输入正确名字");
        return null;
    }

    if ((/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(a[1].value)){
        aphone = a[1].value;
    }else{
        alert("请输入正确的手机号");
        return null;
    }
    if (b.innerText!="选择省份/自治区"){
        var strings = b.innerHTML.split("/");
        sheng = strings[0];
        shi = strings[1];
        qu = strings[2];
    }else {
        alert("请选择省市区");
        return null;
    }

    if (c.value!=""){
        addressname= c.value;
    }else{
        alert("请输入详细地址");
        return null;
    }

    $.ajax({
        url: "/Shippingaddress?v=bianjiAddress",
        type: "post",
        data: {
            jaid,
            addressname,
            sheng,
            shi,
            qu,
            aname,
            aphone
        },
        success:(r=>{
            let a = document.querySelector(".newaddressbox");
            if (r=="地址编辑成功！"){
                let e = document.querySelector(".nad-body-btn2");
                e.removeEventListener('click',queding2);
                e.onclick = queding;
                let f = document.querySelector(".nad-title-text");
                f.innerHTML = "新建收货地址";
                alert(r);
                $("#adressd").load(location.href + " #adressd>*","");
                a.style.display = "none";
            }else{
                alert(r);
            }
        })
    })

}



function cadbianjibtn(aid){

    $.ajax({
        url:"/Shippingaddress?v=selectOne",
        type:"post",
        data:{
            aid
        },
        dataType:"json",
        success:(r)=>{
            newaddress();
            let a = document.querySelectorAll(".nad-body-input1");
            let b = document.querySelector(".nad-body-input2");
            let c = document.querySelector(".nad-body-input3");
            let d = document.querySelector(".nad-title-text");
            a[0].value = r.aname;
            a[1].value = r.aphone;
            b.innerHTML = r.sheng+"/"+r.shi+"/"+r.qu;
            c.value = r.addressname;
            d.innerHTML = "编辑收货地址";
            let e = document.querySelector(".nad-body-btn2");
            console.log(r.aid);
            qaid = r.aid;
            e.onclick = false;
            e.addEventListener('click',queding2);
        }
    })
}



